home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / allison / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  780 b   |  37 lines

  1. LISTING 12 - Uses strstr to find substrings
  2.  
  3. /* find.c:    Extract lines from a file */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. main(int argc, char *argv[])
  10. {
  11.     char line[BUFSIZ];
  12.     char *search_str;
  13.     int lineno = 0;
  14.  
  15.     if (argc == 1)
  16.         return EXIT_FAILURE;   /* Search string required */
  17.     else
  18.         search_str = argv[1];
  19.  
  20.     while (gets(line))
  21.     {
  22.         ++lineno;
  23.         if (strstr(line,search_str))
  24.             printf("%d:  %s\n",lineno,line);
  25.     }
  26.  
  27.     return EXIT_SUCCESS;
  28. }
  29.  
  30. /* Results from the command "find str <find.c": */
  31. 5:  #include <string.h>
  32. 12:      char *search_str;
  33. 16:          return EXIT_FAILURE;
  34. 18:          search_str = argv[1];
  35. 23:          if (strstr(line,search_str))
  36.  
  37.